using JGSY.CMS.LowCode.Platform.Application.Interfaces;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;

namespace JGSY.CMS.LowCode.Platform.Infrastructure.Services
{
    /// <summary>
    /// 登录历史服务实现
    /// 提供登录历史记录管理的业务逻辑实现，包括查询、添加、更新和删除操作
    /// </summary>
    public class LoginHistoryService : ILoginHistoryAppService
    {
        /// <summary>
        /// 登录历史仓储接口
        /// </summary>
        private readonly Domain.Interfaces.ILoginHistoryRepository _repo;
        
        /// <summary>
        /// 初始化登录历史服务实例
        /// </summary>
        /// <param name="repo">登录历史仓储接口实例</param>
        public LoginHistoryService(Domain.Interfaces.ILoginHistoryRepository repo)
        {
            _repo = repo;
        }
        /// <summary>
        /// 获取所有登录历史记录
        /// 检索系统中的所有登录历史记录并转换为DTO格式
        /// </summary>
        /// <returns>登录历史记录DTO列表</returns>
        public async Task<IEnumerable<Application.DTOs.LoginHistoryDto>> GetAllAsync()
        {
            var entities = await _repo.GetAllAsync();
            return entities.Select(e => new Application.DTOs.LoginHistoryDto
            {
                Id = e.Id,
                UserId = e.UserId   ,
                LoginTime = e.CreatedAt,
                IpAddress = e.IpAddress,
                Location = e.Location ?? string.Empty,
                DeviceId = !string.IsNullOrEmpty(e.DeviceInfo) ? e.DeviceInfo : (e.SessionId ?? string.Empty)
            });
        }
        /// <summary>
        /// 根据ID获取登录历史记录
        /// 查找指定标识符的登录历史记录详细信息
        /// </summary>
        /// <param name="id">登录历史记录标识符</param>
        /// <returns>登录历史记录DTO，如果不存在则返回null</returns>
        public async Task<Application.DTOs.LoginHistoryDto?> GetByIdAsync(long id)
        {
            var entity = await _repo.GetByIdAsync(id);
            if (entity == null) return null;
            return new Application.DTOs.LoginHistoryDto
            {
                Id = entity.Id,
                UserId = entity.UserId  ,
                LoginTime = entity.CreatedAt,
                IpAddress = entity.IpAddress,
                Location = entity.Location ?? string.Empty,
                DeviceId = !string.IsNullOrEmpty(entity.DeviceInfo) ? entity.DeviceInfo : (entity.SessionId ?? string.Empty)
            };
        }
        /// <summary>
        /// 添加登录历史记录
        /// 创建新的登录历史记录并保存到数据库
        /// </summary>
        /// <param name="dto">登录历史记录DTO</param>
        /// <returns>添加操作的异步任务</returns>
        public async Task AddAsync(Application.DTOs.LoginHistoryDto dto)
        {
            var entity = new Domain.Entities.LoginHistory
            {
                Id = dto.Id,
                UserId = dto.UserId,
                CreatedAt = dto.LoginTime,
                IpAddress = dto.IpAddress,
                Location = dto.Location,
                DeviceInfo = dto.DeviceId
            };
            await _repo.AddAsync(entity);
        }

        /// <summary>
        /// 更新登录历史记录
        /// 更新指定登录历史记录的信息
        /// </summary>
        /// <param name="id">登录历史记录标识符</param>
        /// <param name="dto">更新后的登录历史记录DTO对象</param>
        /// <returns>更新操作的异步任务</returns>
        public async Task UpdateAsync(long id, Application.DTOs.LoginHistoryDto dto)
        {
            var entity = new Domain.Entities.LoginHistory
            {
                Id = dto.Id,
                UserId = dto.UserId,
                CreatedAt = dto.LoginTime,
                IpAddress = dto.IpAddress,
                Location = dto.Location,
                DeviceInfo = dto.DeviceId
            };
            await _repo.UpdateAsync(id, entity);
        }

        /// <summary>
        /// 删除登录历史记录
        /// 从系统中移除指定的登录历史记录信息
        /// </summary>
        /// <param name="id">登录历史记录标识符</param>
        /// <returns>删除操作的异步任务</returns>
        public async Task DeleteAsync(long id)
        {
            await _repo.DeleteAsync(id);
        }
    }
}
